package Controller

import (
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"log"
	"m/Global"
	"m/model"
	"net/http"
)

func RegisterFamily(context *gin.Context) {
	// 使用结构体来获取参数
	var requsetFamily = model.Family{}
	json.NewDecoder(context.Request.Body).Decode(&requsetFamily)
	fmt.Println("获取到的数据：", requsetFamily.Tno, requsetFamily.Tname, requsetFamily.Tlevel, requsetFamily.Tcharacter, requsetFamily.Taddress, requsetFamily.Tdie, requsetFamily.Tmarry, requsetFamily.Tpartner, requsetFamily.Tlife, requsetFamily.Tbirth, requsetFamily.Tchild, requsetFamily.Thonor)

	// 获取参数
	fmt.Println("开始获取教师参数...")
	tno := requsetFamily.Tno
	tname := requsetFamily.Tname
	tlevel := requsetFamily.Tlevel
	tcharacter := requsetFamily.Tcharacter
	taddress := requsetFamily.Taddress
	tdie := requsetFamily.Tdie
	tmarry := requsetFamily.Tmarry
	tpartner := requsetFamily.Tpartner
	tlife := requsetFamily.Tlife
	tbirth := requsetFamily.Tbirth
	tchild := requsetFamily.Tchild
	thonor := requsetFamily.Thonor

	// 数据验证
	fmt.Println("开始验证家族成员数据...")
	fmt.Println("成员编号：", tno, "成员名字：", tname, "成员等级：", tlevel, "成员特征：", tcharacter, "家庭地址：", taddress, "死亡时间：", tdie, "结婚日期：", tmarry, "配偶信息：", tpartner, "生平事迹：", tlife, "出生日期：", tbirth, "子女信息：", tchild, "家谱ID：", thonor)
	if len(tno) == 0 {
		//context.JSON(http.StatusUnprocessableEntity, gin.H{"code":421, "msg":"教师号不可为空！"})
		fmt.Println("发现成员编号为空")
		context.JSON(200, gin.H{
			"code": 200,
			"msg":  "成员编号不能为空",
		})
		return
	}
	if len(tname) == 0 {
		//context.JSON(http.StatusUnprocessableEntity, gin.H{"code":422, "msg":"教师名不可为空！"})
		fmt.Println("发现成员名字为空")
		context.JSON(200, gin.H{
			"code": 200,
			"msg":  "成员名字不能为空",
		})
		return
	}
	fmt.Println(tno, tname, tlevel, tcharacter, taddress)
	log.Println(tno, tname, tlevel, tcharacter, taddress)
	// 判断教师号是否存在
	//tempTno, err := strconv.Atoi(tno)
	if isTnoExists(Global.Db, tno) == true {
		//context.JSON(http.StatusUnprocessableEntity, gin.H{"code":422, "msg":"教师号已经存在！"})
		// 直接返回，不执行下面的create了
		context.JSON(200, gin.H{
			"code": 200,
			"msg":  "成员编号已经存在",
		})
		return
	}
	// 创建成员
	//if err != nil {
	//	fmt.Println("错误")
	//	return
	//}
	newFamily := model.Family{
		Tno:        tno,
		Tname:      tname,
		Tlevel:     tlevel,
		Tcharacter: tcharacter,
		Taddress:   taddress,
		Tdie:       tdie,
		Tmarry:     tmarry,
		Tpartner:   tpartner,
		Tlife:      tlife,
		Tbirth:     tbirth,
		Tchild:     tchild,
		Thonor:     thonor,
	}

	if err := Global.Db.Create(&newFamily).Error; err != nil {
		context.JSON(201, gin.H{
			"code": 201,
			"msg":  "插入家族成员失败",
		})
	} else {
		context.JSON(200, gin.H{
			"code": 200,

			"msg": "插入家族成员成功",
		})
	}
}

func ShowFamily(context *gin.Context) {
	fmt.Println("进入showfamily")
	var family []model.Family
	err := Global.Db.Find(&family).Error
	if err != nil {
		fmt.Println("查询全体家族成员出错！")
		return
	}
	fmt.Println("数据库查到的全体家族成员信息：", family)

	//token, err := Global.ReaeaseTokec3(family)
	context.JSON(http.StatusOK, family)
}

func CountFamily(context *gin.Context) {
	// 调用存储过程
	var count int
	Global.Db.Raw("select count_family()").First(&count)
	fmt.Println("成员数量：", count)
	context.JSON(200, gin.H{
		"code": 200,
		"msg":  "返回家族成员数量成功",
		"data": count,
	})
}

func EditFamily(context *gin.Context) {
	fmt.Println("进入editfamily")
	var requestFamily = model.Family{}
	json.NewDecoder(context.Request.Body).Decode(&requestFamily)
	fmt.Println("即将编辑家族成员编号：", requestFamily.Tno)

	if len(requestFamily.Tno) < 1 {
		fmt.Println("家族成员编号不能为空！", len(requestFamily.Tno))
		context.JSON(200, gin.H{
			"code": 200,
			"msg":  "家族成员编号不能为空",
		})
		return
	}
	if len(requestFamily.Tname) < 1 {
		fmt.Println("成员姓名不能为空！", len(requestFamily.Tname))
		context.JSON(200, gin.H{
			"code": 200,
			"msg":  "成员姓名不能为空",
		})
		return
	}
	//if len(requestFamily.Tlevel) < 1 {
	//	fmt.Println("家族成员等级不能为空！", len(requestFamily.Tlevel))
	//	context.JSON(200, gin.H{
	//		"code": 200,
	//		"msg":  "家族成员等级不能为空",
	//	})
	//	return
	//}
	//if len(requestFamily.Tcharacter) < 1 {
	//	fmt.Println("成员特长不能为空！", len(requestFamily.Tcharacter))
	//	context.JSON(200, gin.H{
	//		"code": 200,
	//		"msg":  "成员特长不能为空",
	//	})
	//	return
	//}

	//// 教师号是否存在
	//var family model.Family
	//Global.Db.Where("tno = ?", requestFamily.Tno).First(&family)
	//fmt.Println("family.Tno=", family.Tno)
	//if family.Tno == "" {
	//	context.JSON(200, gin.H{
	//		"code": 200,
	//		"msg":  "找不到此家族成员",
	//	})
	//	return
	//}
	//Global.Db.Save(&requestFamily)
	//Global.Db.Model(&requestFamily).Updates(model.Family{
	//	Tno: requestFamily.Tno,
	//	Tname: requestFamily.Tname,
	//	Tlevel: requestFamily.Tlevel,
	//	Tcharacter: requestFamily.Tcharacter,
	//})
	err := Global.Db.Where("tno = ?", requestFamily.Tno).Updates(&requestFamily).Error
	if err != nil {
		fmt.Println("更新成员出错")
		context.JSON(200, gin.H{
			"code": 200,
			"msg":  "更新成员出错",
		})
		return
	}

	context.JSON(200, gin.H{
		"code": 200,
		"msg":  "更新成功",
	})

}

func DeleteFamily(context *gin.Context) {
	fmt.Println("进入deletefamily")
	var requestFamily = model.Family{}
	json.NewDecoder(context.Request.Body).Decode(&requestFamily)
	fmt.Println("即将删除成员编号：", requestFamily.Tno)

	if len(requestFamily.Tno) < 1 {
		fmt.Println("成员编号不能为空！", len(requestFamily.Tno))
		context.JSON(200, gin.H{
			"code": 200,
			"msg":  "成员编号不能为空",
		})
		return
	}
	// 教师号是否存在
	var family model.Family
	Global.Db.Where("tno = ?", requestFamily.Tno).First(&family)
	fmt.Println("family.Tno=", family.Tno)
	if family.Tno == "" {
		context.JSON(200, gin.H{
			"code": 200,
			"msg":  "找不到此成员",
		})
		return
	}
	// 首先删除有这个老师的课程
	var relation = model.Relation{
		Tno: requestFamily.Tno,
	}
	var err error
	err = Global.Db.Where("tno = ?", requestFamily.Tno).Delete(&relation).Error
	if err != nil {
		context.JSON(200, gin.H{
			"code": 200,
			"msg":  "删除此教师所教授的课程出错",
		})
		return
	}

	// 其次删除这个老师
	err = Global.Db.Where("tno = ?", requestFamily.Tno).Delete(&family).Error
	if err != nil {
		context.JSON(200, gin.H{
			"code": 200,
			"msg":  "删除成员出错",
		})
		return
	}

	context.JSON(200, gin.H{
		"code": 200,
		"msg":  "删除成功",
	})
}

func ShowFamilySelect(context *gin.Context) {
	fmt.Println("进入showteacheerselect")
	var requestFamily = model.Family{}
	json.NewDecoder(context.Request.Body).Decode(&requestFamily)
	fmt.Println("即将搜索成员编号：", requestFamily.Tno)

	if len(requestFamily.Tno) < 1 {
		fmt.Println("成员编号不能为空！", len(requestFamily.Tno))
		context.JSON(http.StatusUnprocessableEntity, gin.H{
			"code": 422,
			"msg":  "成员编号为空",
		})
		return
	}
	// 家族成号是否存在
	var family []model.Family
	Global.Db.Where("tno = ?", requestFamily.Tno).First(&family)
	fmt.Println("family.Tno=", family[0].Tno)
	if family[0].Tno == "" {
		context.JSON(http.StatusUnprocessableEntity, gin.H{
			"code": 422,
			"msg":  "找不到此家族成员！",
		})
		return
	}

	context.JSON(http.StatusOK, family)

}

func ShowFamilyDelete(context *gin.Context) {
	fmt.Println("进入showfamilydelete")
	//var requesetFamily = model.Family{}
}

func isTnoExists(db *gorm.DB, tno string) bool {
	var family model.Family
	err := db.Where("tno = ?", tno).Find(&family).Error
	if err != nil {
		fmt.Println("err执行")
		return false
	}
	fmt.Println("数据库返回的教师号：", family.Tno)
	if family.Tno != "" { // 找到重复的教师号
		fmt.Println("找到重复的教师号为：", family.Tno)
		fmt.Println("成员执行了if")
		return true
	} else {
		fmt.Println("成员执行了else")
		return false
	}
}
